/**
 * @author of2198
 * @date 2017/5/3.
 * @description
 */
import { getEnvXSite } from "x-site-util";
import { getSystemCodeZip, systemCodeZipMap } from "./const-value";

let systemCodeZip = getSystemCodeZip();
export const applyTpl = (templateInfo, sc = systemCodeZip) => {
  let pageType = templateInfo.pageType;
  let platform = templateInfo.platForm;

  let host = getEnvXSite();
  let pageCode;
  let id = templateInfo.id;
  let rule4PageCode = templateInfo.rule4PageCode;
  // 处理 pageCode
  switch (rule4PageCode) {
    case "multi":
      pageCode = Date.now().toString();
      break;
    case "single":
      pageCode = pageType;
      break;
    default:
      break;
  }

  // 处理pathname
  let pathname =
    pageType === "index"
      ? `/newIndex/${platform}/${pageCode}`
      : `/inner/${platform}/${pageType}/${pageCode}`;
  // 处理search
  let search = { id, sc };
  // 生成url
  const url = host + pathname + "?" + serialize(search);
  return url;
};

/**
 * 序列话对象
 * @return {String} eg: a=1&b=foo
 */
const serialize = param => {
  const s = Object.keys(param)
    .map(key => {
      let value = param[key];
      if (value == null) return null;
      return key + "=" + value;
    })
    .filter(item => item != null);
  return s.join("&");
};

/**
 * 是否是多页面
 * @desc 多页面使用new Date(),
 * 非多页面使用 pageCode
 * @param platform
 * @param type
 */
const isMultiPage = (platform, type) => {
  const pageStore = {
    pc: ["index", "custom", "poster", "article", "order"],
    weixin: ["index", "custom", "poster", "article", "liveShow"],
    app: ["index"]
  };
  return pageStore[platform].indexOf(type) > -1;
};
